﻿using Leo.Awesome.AppPlatform.Enums;
using Leo.Awesome.AppPlatform.Localization;
using Leo.Awesome.AppPlatform.Repostories;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Authorization.Permissions;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Localization;

namespace Leo.Awesome.AppPlatform.DynamicMenus
{
    public class AppDynamicPermissionDefinitionStore : ITransientDependency,
        IDynamicPermissionDefinitionStore
    {
        
        private readonly IMenuRepository menuRepository;
        private readonly IStaticPermissionDefinitionStore staticPermissionDefinitionStore;

        public AppDynamicPermissionDefinitionStore(
           IStaticPermissionDefinitionStore staticPermissionDefinitionStore,
            IMenuRepository menuRepository         
            )
        {
            this.staticPermissionDefinitionStore = staticPermissionDefinitionStore;
            this.menuRepository = menuRepository;
        }
        public async Task<IReadOnlyList<PermissionGroupDefinition>> GetGroupsAsync()
        {
            var result= await staticPermissionDefinitionStore.GetGroupsAsync();
            var dynamincPermissionGroup = result.FirstOrDefault(x => x.Name == AppPlatfromApplcationContractsConsts.DynamicMenusGroup);        
            var menus = await menuRepository.GetListAsync(x =>
            x.MenuType == MenuType.MenuItem && x.Display);

            foreach (var item in menus)
            {
                if (string.IsNullOrEmpty(item.Permission))
                {
                    continue;
                }
                var permissionDefinition = await staticPermissionDefinitionStore.GetOrNullAsync(item.Permission);
                if (permissionDefinition == null &&
                    !dynamincPermissionGroup.Permissions.Any(x => x.Name == item.Permission))
                {
                    dynamincPermissionGroup.AddPermission(item.Permission, L(item.Name));
                }
            }
            var menuItemPermissionList = new List<PermissionGroupDefinition> { dynamincPermissionGroup };
            return menuItemPermissionList.AsReadOnly();
          
        }

        public async Task<PermissionDefinition> GetOrNullAsync(string name)
        {
            var result = await staticPermissionDefinitionStore.GetGroupsAsync();
            var dynamincPermissionGroup = result.FirstOrDefault(x => x.Name == AppPlatfromApplcationContractsConsts.DynamicMenusGroup);
            return dynamincPermissionGroup.GetPermissionOrNull(name);
        }

        public async Task<IReadOnlyList<PermissionDefinition>> GetPermissionsAsync()
        {
            var result = await staticPermissionDefinitionStore.GetGroupsAsync();
            var dynamincPermissionGroup = result.FirstOrDefault(x => x.Name == AppPlatfromApplcationContractsConsts.DynamicMenusGroup);
            return dynamincPermissionGroup.Permissions;
        }

        private static LocalizableString L(string name)
        {
            return LocalizableString.Create<AppPlatformResource>(name);
        }
    }
}
